{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 智能招聘面试模拟系统\n",
    "\n",
    "本讲义结合项目代码与文档，梳理系统架构、核心技术要点及关键实现示例，适用于课程讲解与实战演练。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 系统架构与技术选型\n",
    "- 前端：Vue.js + Bootstrap\n",
    "- 后端：Flask RESTful API\n",
    "- 数据库：SQLite\n",
    "- AI服务：GLM-4-plus（大语言模型）、Whisper（语音识别）\n",
    "- 定时任务：schedule库\n",
    "\n",
    "![系统架构图](./images/interview-arch.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 数据库设计与初始化\n",
    "\n",
    "核心表结构包括岗位、候选人、面试、面试问题、面试报告等。\n",
    "\n",
    "![数据库表设计](./images/db-er.png)\n",
    "\n",
    "**示例：数据库初始化**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "conn = sqlite3.connect('interview_system.db')\n",
    "cursor = conn.cursor()\n",
    "# 创建岗位表\n",
    "cursor.execute('''\n",
    "CREATE TABLE IF NOT EXISTS jobs (\n",
    "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n",
    "    title TEXT,\n",
    "    description TEXT\n",
    ")\n",
    "''')\n",
    "# 创建候选人表\n",
    "cursor.execute('''\n",
    "CREATE TABLE IF NOT EXISTS candidates (\n",
    "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n",
    "    name TEXT,\n",
    "    resume TEXT\n",
    ")\n",
    "''')\n",
    "conn.commit()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Flask后端API核心示例\n",
    "\n",
    "![API设计](./images/api-list.png)\n",
    "\n",
    "**岗位管理API示例**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask, request, jsonify\n",
    "app = Flask(__name__)\n",
    "@app.route('/api/jobs', methods=['POST'])\n",
    "def create_job():\n",
    "    data = request.json\n",
    "    # 省略数据库插入逻辑\n",
    "    return jsonify({'msg': 'Job created', 'data': data})\n",
    "# app.run(port=8000)  # 启动服务（实际部署时使用）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. AI面试问题与评估自动生成\n",
    "\n",
    "集成大语言模型API，自动生成面试问题和评估报告。\n",
    "\n",
    "**示例：调用OpenAI/GLM API生成面试问题**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "# 初始化OpenAI客户端\n",
    "client = OpenAI(\n",
    "    api_key=os.getenv(\"OPENAI_API_KEY\"),\n",
    "    base_url=os.getenv(\"OPENAI_BASE_URL\")\n",
    ")\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "        model=\"gpt-4\", # 或其他适合的模型=\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"你是一名专业的招聘面试官，请根据岗位要求和候选人简历生成5个针对性的技术面试问题，每个问题附带评分标准,返回标准的json格式。\"},\n",
    "            {\"role\": \"user\", \"content\": f\"岗位名称: {position_name}\\n岗位要求: {requirements}\\n岗位职责: {responsibilities}\\n候选人简历: {resume_text}\\n\\n请生成10个面试问题和评分标准，JSON格式参考 {json_format} ，每个问题满分10分。\"}\n",
    "        ],\n",
    "        response_format={\"type\": \"json_object\"},\n",
    "        stream = False\n",
    "    )\n",
    "    \n",
    " # 解析响应内容\n",
    "questions_json = response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 定时任务与自动化处理\n",
    "\n",
    "使用schedule库定时检查并处理待生成的面试问题和报告。\n",
    "\n",
    "**示例：每5分钟自动执行任务**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import schedule\n",
    "import time\n",
    "def job():\n",
    "    print(\"自动生成面试问题和报告...\")\n",
    "schedule.every(5).minutes.do(job)\n",
    "# while True:\n",
    "#     schedule.run_pending()\n",
    "#     time.sleep(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 语音面试与Whisper语音识别\n",
    "\n",
    "**示例：调用Whisper模型进行语音转写**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import whisper\n",
    "model = whisper.load_model('base')\n",
    "result = model.transcribe('audio.mp3')\n",
    "print(result['text'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. 前后端联动与接口联调\n",
    "\n",
    "前端通过HTTP请求与后端API交互，完成岗位、候选人、面试等管理。\n",
    "\n",
    "**示例：前端fetch请求调用API**\n",
    "```javascript\n",
    "fetch('/api/jobs', {\n",
    "  method: 'POST',\n",
    "  headers: { 'Content-Type': 'application/json' },\n",
    "  body: JSON.stringify({ title: 'Python开发', description: '负责后端开发' })\n",
    "})\n",
    "  .then(res => res.json())\n",
    "  .then(data => console.log(data));\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**练习建议：**\n",
    "- 尝试扩展岗位表结构，增加更多字段\n",
    "- 实现面试流程的完整API\n",
    "- 集成更多AI模型或优化现有流程"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
